﻿namespace HEFramework
{
    /// <summary>
    ///
    /// 文件
    /// 
    /// ----------------------------------------
    /// 作者 hep
    /// 创建时间：2023-12-19 16:35:10
    /// ----------------------------------------
    /// </summary>
    public class File
    {
        private static IFileStream fileStream;

        public static IFileStream FileStream
        {
            get
            {
                if (fileStream == null)
                {
                    if (AssetBundleSettingScriptableObject.Instance.Domain == eDomain.Streaming)
                    {
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS
                        fileStream = new CommonFileStream();

#elif UNITY_ANDROID
                        fileStream = new AndroidStreamingFileSteam();
#endif
                    }
                    else
                    {
                        fileStream = new CommonFileStream();
                    }
                }

                return fileStream;
            }
        }


        /// <summary>
        /// 写入字节（同步）
        /// </summary>
        /// <param name="_filePath"></param>
        /// <param name="_bytes"></param>
        /// <param name="_aes"></param>
        public static void WriteBytesSyn(string _filePath, byte[] _bytes, Aes _aes = null)
        {
            byte[] bytes = _bytes;

            if (bytes == null)
            {
                return;
            }

            if (_aes != null)
            {
                bytes = _aes.EncryptSyn(_bytes);
            }

            FileStream.WriteBytesSyn(_filePath, bytes);
        }


        /// <summary>
        /// 写入字节（异步）
        /// </summary>
        /// <param name="_filePath"></param>
        /// <param name="_bytes"></param>
        /// <param name="_aes"></param>
        /// <param name="_cancellationToken"></param>
        /// <param name="_bufferSize"></param>
        public static async HETask WriteBytesAsync(string _filePath, byte[] _bytes, Aes _aes = null, HECancellationToken _cancellationToken = null, int _bufferSize = 4096)
        {
            byte[] bytes = _bytes;

            if (bytes == null)
            {
                return;
            }

            if (_aes != null)
            {
                await _aes.EncryptAsync(bytes, _cancellationToken, _bufferSize);
            }

            await FileStream.WriteBytesAsync(_filePath, bytes, _cancellationToken, _bufferSize);
        }


        /// <summary>
        /// 读取字节（同步）
        /// </summary>
        /// <param name="_filePath">文件读取路径</param>
        /// <param name="_aes">解密器</param>
        /// <returns></returns>
        public static byte[] ReadBytesSyn(string _filePath, Aes _aes = null)
        {
            byte[] bytes = FileStream.ReadBytesSyn(_filePath);

            if (bytes == null)
            {
                return null;
            }

            if (_aes != null)
            {
                bytes = _aes.DecryptSyn(bytes);
            }

            return bytes;
        }


        /// <summary>
        /// 读取字节（异步）
        /// </summary>
        /// <param name="_filePath"></param>
        /// <param name="_aes"></param>
        /// <param name="_cancellationToken"></param>
        /// <param name="_bufferSize"></param>
        /// <returns></returns>
        public static async HETask<byte[]> ReadBytesAsync(string _filePath, Aes _aes = null, HECancellationToken _cancellationToken = null, int _bufferSize = 4096)
        {
            byte[] bytes = await FileStream.ReadBytesAsync(_filePath, _cancellationToken, _bufferSize);

            if (bytes == null)
            {
                return null;
            }

            if (_aes != null)
            {
                bytes = await _aes.DecryptAsync(bytes, _cancellationToken, _bufferSize);
            }

            return bytes;
        }


        /// <summary>
        /// 检测文件是否存在
        /// </summary>
        /// <param name="_filePath">文件路径</param>
        /// <returns></returns>
        public static bool CheckFile(string _filePath)
        {
            return FileStream.CheckFile(_filePath);
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="_filePath">文件路径</param>
        public static bool DeleteFile(string _filePath)
        {
            return FileStream.DeleteFile(_filePath);
        }

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="_path"></param>
        /// <returns></returns>
        public static bool CreateDirectory(string _path)
        {
            return FileStream.CreateDirectory(_path);
        }

        /// <summary>
        /// 删除文件夹（文件夹本身以及内部所有内容）
        /// </summary>
        /// <param name="_path"></param>
        /// <returns></returns>
        public static bool DeleteDirectory(string _path)
        {
            return FileStream.DeleteDirectory(_path);
        }
    }
}